package com.feng.controller;

import com.feng.domain.ResponseResult;
import com.feng.domain.dto.AddArticleDto;
import com.feng.domain.dto.AdminUpdateGetArticleDto;
import com.feng.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/content/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @PreAuthorize("@ps.hasPermission('content:article:writer')")
    @PostMapping
    public ResponseResult addArticle(@RequestBody AddArticleDto addArticleDto){
        return articleService.addArticle(addArticleDto);
    }

    @PreAuthorize(("@ps.hasPermission('content:article:list')"))
    @GetMapping("/list")
    public ResponseResult list(Integer pageNum, Integer pageSize, String title, String summary){
        return articleService.selectList(pageNum, pageSize, title, summary);
    }

    @PreAuthorize(("@ps.hasPermission('content:article:list')"))
    @GetMapping("/{id}")
    public ResponseResult getList(@PathVariable("id") Long id){
        return articleService.getList(id);
    }

    @PreAuthorize(("@ps.hasPermission('content:article:list')"))
    @PutMapping
    public ResponseResult updateArticle(@RequestBody AdminUpdateGetArticleDto articleDto){
        return articleService.updateArticle(articleDto);
    }

    @PreAuthorize(("@ps.hasPermission('content:article:list')"))
    @DeleteMapping("/{id}")
    public ResponseResult deleteArticle(@PathVariable("id")List<Long> idList){
        return articleService.deleteArticle(idList);
    }
}
